package com.niuwa.p2p.action.dsfmanager;

import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.DictUtil;
import com.niuwa.p2p.core.utils.StringUtil;
import com.niuwa.p2p.entity.fund.ChlAcctRule;
import com.niuwa.p2p.entity.fund.CustAcctRule;
import com.niuwa.p2p.entity.fund.PayPartner;
import com.niuwa.p2p.entity.fund.PayPartnerDetail;
import com.niuwa.p2p.entity.fund.PayPartnerFile;
import com.niuwa.p2p.entity.nwfile.NWFile;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.fund.IPayPartnerService;
import com.niuwa.p2p.service.protocol.IProtocolService;
import com.niuwa.p2p.vo.fund.PayPartnerForm;
import com.opensymphony.xwork2.ActionContext;

public class ThirdPartyAction extends JsonAction {
	
	/** 用户Servcie spring扫包注入 */
	@Autowired
	private IPayPartnerService partnerService;
	
	@Autowired
	private IProtocolService protocolService;
	
	/** 添加变更记录上传文件ID*/
	private List<Long> protFileID;
	/** 添加机构上传文件ID*/
	private List<Long> payPartnerFileID;
	/** 合作机构变更明细ID*/
	private Long detailId;
	/** 操作类型*/  
	private int type;
	/** 机构ID*/
	private String partnerCode;
	/** 协议相关附件*/
	private List<NWFile> detailAttachment;
	/** 变更记录信息*/
	private List<PayPartnerDetail> partnerDetails;
	/** 合作机构相关联的协议文件 */
	private List<PayPartnerFile> partnerFiles;
	/** 机构信息*/
	private PayPartner partner;
	/** 机构变更记录*/
	private PayPartnerDetail partnerDetail;
	/** 添加*/ 
	private int ADD = 1;
	/** 编辑*/ 
	private int UPDATE = 3;
	
	private CustAcctRule custr;
	
	private ChlAcctRule chlr;
	

	public List<PayPartnerFile> getPartnerFiles() {
		return partnerFiles;
	}

	public void setPartnerFiles(List<PayPartnerFile> partnerFiles) {
		this.partnerFiles = partnerFiles;
	}

	public CustAcctRule getCustr() {
		return custr;
	}

	public void setCustr(CustAcctRule custr) {
		this.custr = custr;
	}

	public ChlAcctRule getChlr() {
		return chlr;
	}

	public void setChlr(ChlAcctRule chlr) {
		this.chlr = chlr;
	}

	public List<Long> getPayPartnerFileID() {
		return payPartnerFileID;
	}

	public void setPayPartnerFileID(List<Long> payPartnerFileID) {
		this.payPartnerFileID = payPartnerFileID;
	}

	public PayPartnerDetail getPartnerDetail() {
		return partnerDetail;
	}

	public void setPartnerDetail(PayPartnerDetail partnerDetail) {
		this.partnerDetail = partnerDetail;
	}

	public List<Long> getProtFileID() {
		return protFileID;
	}

	public void setProtFileID(List<Long> protFileID) {
		this.protFileID = protFileID;
	}

	public String getPartnerCode() {
		return partnerCode;
	}

	public void setPartnerCode(String partnerCode) {
		this.partnerCode = partnerCode;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
	
	public Long getDetailId() {
		return detailId;
	}

	public void setDetailId(Long detailId) {
		this.detailId = detailId;
	}

	public List<PayPartnerDetail> getPartnerDetails() {
		return partnerDetails;
	}

	public void setPartnerDetails(List<PayPartnerDetail> partnerDetails) {
		this.partnerDetails = partnerDetails;
	}
	public List<NWFile> getDetailAttachment() {
		return detailAttachment;
	}
	public void setDetailAttachment(List<NWFile> detailAttachment) {
		this.detailAttachment = detailAttachment;
	}
	public PayPartner getPartner() {
		return partner;
	}

	public void setPartner(PayPartner partner) {
		this.partner = partner;
	}

	/**
	 * 查看第三方机构列表
	 * @return
	 */
	@Action("gotoThirdPartyList")
	public String gotoBorrowList() {
		
		return "thirdPartyList";
	}

	/**
	 * 查看第三方机构详情
	 * @return
	 */
	@Action("showThirdDetails")
	public String showThirdDetails() {
		
		ActionContext context = ActionContext.getContext();
		
		if (null != partnerCode && !"".equals(partnerCode)) {
			//机构基本信息
			partner = partnerService.selectByPrimaryKey(partnerCode);
			context.put("partner", partner);
			//机构相关变更记录信息
			partnerDetails = partnerService.selectDetailByPartnerNo(partnerCode);
			context.put("partnerDetails", partnerDetails);
			//机构协议附件信息
			partnerFiles = partnerService.selectFileByPartnerNo(partnerCode);
			context.put("partnerFiles", partnerFiles);
			//用户规则信息
			custr = partnerService.selectCustRuleByPartnerNo(partnerCode);
			context.put("custr", custr);
			//渠道规则信息
			chlr = partnerService.selectChlRuleByPartnerNo(partnerCode);
			context.put("chlr", chlr);
		}
		return "showThirdDetails";
	}
	
	/**
	 * 跳转到添加机构页面
	 * 
	 * @return 投资产品模板视图
	 * 
	 */	
	@Action("gotoAddPayPartner")
	public String gotoAddPayPartner() {
		ActionContext context = ActionContext.getContext();
		context.put("type", type);
		return "addOrUpdateThirdDetails";
	}
	
	/**
	 * 添加合作机构
	 * @return
	 */
	@Action("addPayPartner")
	public String addPayPartner() {
		JsonResult result = new JsonResult();
		// 获取用户ID
		long userId = SessionUtil.getSessionUser().getId();
		try {
			//判断是否上传协议文件
			String errmsg = partnerFiledCheck();
			if (StringUtil.isEmpty(errmsg) == false) {
				result.setSuccess(false);
				result.setMsg(errmsg);
				this.setResult(result);
				return NONE;
			}
			//判断机构名称是否存在
			String partnerName = partner.getPartnerName();
			int num = partnerService.selectPartnerByPartnerName(partnerName);
			if(num > 0){
				result.setSuccess(false);
				result.setMsg("机构名称已存在");
			}else{
				//添加机构信息
				PayPartnerForm form = new PayPartnerForm();
				form.setPartner(partner);
				form.setCustr(custr);
				form.setChlr(chlr);
				partnerService.insert(form,payPartnerFileID,userId);
				result.setSuccess(true);
				result.setMsg("操作成功");
			}
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg("操作失败");
		}
		this.setResult(result);
		return NONE;
	}
	
	/**
	 * 修改合作机构信息
	 * @return
	 */
	@Action("updatePayPartner")
	public String updatePayPartner() {
		JsonResult result = new JsonResult();
		// 获取用户ID
		long userId = SessionUtil.getSessionUser().getId();
		try {
			//判断是否上传协议文件
			String errmsg = partnerFiledCheck();
			if (StringUtil.isEmpty(errmsg) == false) {
				result.setSuccess(false);
				result.setMsg(errmsg);
				this.setResult(result);
				return NONE;
			}
			//修改机构信息
			PayPartnerForm form = new PayPartnerForm();
			partner.setPartnerCode(partnerCode);
			form.setPartner(partner);
			form.setCustr(custr);
			form.setChlr(chlr);
			partnerService.update(form,payPartnerFileID,userId);
			result.setSuccess(true);
			result.setMsg("操作成功");
		} catch (Exception e) {
			result.setSuccess(false);
			result.setMsg("操作失败");
		}
		this.setResult(result);
		return NONE;
	}
	
	/**
	 * 跳转到修改机构详情页面
	 * 
	 * @return
	 */
	@Action("updateThirdDetails")
	public String updateThirdDetails(){
		
		ActionContext context = ActionContext.getContext();
		
		//机构基本信息
		partner = partnerService.selectByPrimaryKey(partnerCode);
		context.put("partner", partner);
		//机构相关变更记录信息
		partnerDetails = partnerService.selectDetailByPartnerNo(partnerCode);
		context.put("partnerDetails", partnerDetails);
		//机构协议附件信息
		partnerFiles = partnerService.selectFileByPartnerNo(partnerCode);
		context.put("partnerFiles", partnerFiles);
		//用户规则信息
		custr = partnerService.selectCustRuleByPartnerNo(partnerCode);
		context.put("custr", custr);
		//渠道规则信息
		chlr = partnerService.selectChlRuleByPartnerNo(partnerCode);
		context.put("chlr", chlr);
	
		context.put("type", type);
		return "addOrUpdateThirdDetails";
	}
	
	/**
	 * 获取变更记录相关文件 
	 * @return
	 */
	@Action("gotoDetailFiles")
	public String gotoDetailFiles() {
		ActionContext context = ActionContext.getContext();
		detailAttachment = partnerService.selectPartnerDetailFileByDetailId(detailId);
		context.put("detailAttachment", detailAttachment);
		return "filesDetails";
	}
	
	/**
	 * 获取机构相关文件
	 * @return
	 */
	@Action("gotoPartnerFiles")
	public String gotoPartnerFiles() {
		ActionContext context = ActionContext.getContext();
		detailAttachment = partnerService.selectPartnerFileByPartnerNo(partnerCode);
		context.put("detailAttachment", detailAttachment);
		return "filesDetails";
	}

	
	/**
	 * 机构列表
	 * @return
	 * @throws Exception
	 */
	@Action(value = "thirdPartyList")
	public String thirdPartyList() throws Exception {
		// 获取查询参数
		Map<String, Object> paramMap = getParamMap();
		// 执行查询操作
		PageBean<PayPartner> pagination = partnerService.selectPartnerBySelective(paramMap);
		setObjectReSult(pagination);
		return NONE;
	}
	
	/**
	 * 添加变更记录
	 * @return
	 * @throws Exception
	 */
	@Action(value = "addPartnerDetailFile")
	public String addPartnerDetailFile() throws Exception {
		// 获取用户ID
		long userId = SessionUtil.getSessionUser().getId();
		JsonResult result = new JsonResult();
		String errmsg = partnerDetailFiledCheck();
		if (StringUtil.isEmpty(errmsg) == false) {
			result.setSuccess(false);
			result.setMsg(errmsg);
			this.setResult(result);
			return NONE;
		}
		//添加 
		partnerDetail.setPartnerNo(partnerCode);
		partnerDetail.setOperator(userId);
		int addResult = partnerService.addPartnerDetail(partnerDetail, protFileID);
		if(addResult > 0){
			result.setData(partnerDetail);
			result.setSuccess(true);
			result.setMsg("操作成功");
			this.setResult(result);
		}else{
			result.setSuccess(false);
			result.setMsg("操作失败");
			this.setResult(result);
		}
		return NONE;
	}

	/**
	 * 添加变更记录check
	 * 
	 * @return check信息(无错误:"")
	 */
	private String partnerDetailFiledCheck(){
		final String CHANGE_TYPE = "CHANGE_TYPE";
		//变更说明
		if(StringUtil.isEmpty(partnerDetail.getChangeDesc())) {
			return this.getText("partnerDetail.changeDesc.RequiredString.message");
		} 
		//变更类型
		String changeType = partnerDetail.getChangeType();
		if(StringUtil.isEmpty(changeType)) {
			return this.getText("partnerDetail.changeType.RequiredString.message");
		}
		//数据字典是否存在该类型
		if(StringUtil.isEmpty(DictUtil.getDictNameByTypeAndCode(CHANGE_TYPE, changeType))) {
			return this.getText("partnerDetail.changeType.TypeError.message");
		}
		return "";
	}
	
	/**
	 * 添加/修改合作机构check
	 * @return
	 */
	private String partnerFiledCheck(){
		//用户账户资金规则 --免费充值
		if(StringUtil.isEmpty(custr.getIsRechargeFree())){
			return this.getText("custr.isRechargeFree.RequiredString.message");
		}else if("Y".equalsIgnoreCase(custr.getIsRechargeFree())){
			if(StringUtil.isEmpty(custr.getRechargeLimit())){
				return this.getText("custr.rechargeLimit.RequiredString.message");
			}
		}
		//用户账户资金规则 --免费提现
		if(StringUtil.isEmpty(custr.getIsWithdrawFree())){
			return this.getText("custr.isWithdrawFree.RequiredString.message");
		}else if("Y".equalsIgnoreCase(custr.getIsWithdrawFree())){
			if(StringUtil.isEmpty(custr.getWithdrawLimit())){
				return this.getText("custr.withdrawLimit.RequiredString.message");
			}
		}
		// 渠道账户资金规则 --提现是否需审批
		if(StringUtil.isEmpty(chlr.getIsWithdrawAudit())){
			return this.getText("chlr.isWithdrawAudit.RequiredString.message");
		}
		//  渠道账户资金规则 --发送充值通知
		if(StringUtil.isEmpty(chlr.getIsSendMsg())){
			return this.getText("chlr.isSendMsg.RequiredString.message");
		}
		//  渠道账户资金规则 -- 	可提现部分
		if(StringUtil.isEmpty(chlr.getUsableWithdraw())){
			return this.getText("chlr.usableWithdraw.RequiredString.message");
		}
		//上传的协议文件
		if(payPartnerFileID == null || payPartnerFileID.size() == 0){
			return this.getText("partner.payPartnerFileID.RequiredString.message");
		}
		return "";
	}
	
	
}
